import { loginInfo, userProfile } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = () => {
  return {
    token: getToken(),
    userInfo: {}// 存储用户基本资料
  }
}
const mutations = {
  // 储存token
  setToken(state, token) {
    state.token = token
    // 数据持久化
    setToken(token)
  },
  // 删除token
  delToken(state) {
    state.token = ''
    removeToken()
  },
  // 修改用户信息
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
}
const actions = {
  async getLogin(ctx, data) {
    const token = await loginInfo(data)
    ctx.commit('setToken', token)
  },
  async getUserProfile(ctx) {
    const res = await userProfile()
    console.log(res)
    ctx.commit('setUserInfo', res)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

